package com.bx.imserver.rabbitmq;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.system.UserInfo;
import com.alibaba.fastjson.JSONObject;
import com.bx.imcommon.RabbitConstant;
import com.bx.imcommon.enums.IMCmdType;
import com.bx.imcommon.model.IMRecvInfo;
import com.bx.imserver.netty.processor.AbstractMessageProcessor;
import com.bx.imserver.netty.processor.ProcessorFactory;
import com.bx.imserver.utils.JsonXMLUtils;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @ClassName: MsgReceiver
 * @Description:
 * @Author guochuang
 * @Date 2021/10/25
 * @Version 1.0
 */
@Component
public class MsgReceiver {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 群组信息
     *
     * @param channel
     * @param message
     * @throws Exception
     */
    @RabbitListener(queues = RabbitConstant.QUEUE_A)
    @RabbitHandler
    public void processGroup(Channel channel, @Payload String content, Message message) throws Exception {
        logger.info(">>>>>>>>>>>>>>>>>message:{}", content);
        IMRecvInfo imRecvInfos =  JsonXMLUtils.json2obj(content, IMRecvInfo.class);
        try {
            /**
             * 异步处理消息
             */
           processorMessage(imRecvInfos, IMCmdType.GROUP_MESSAGE.code());
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>error:{}" , e.getMessage());
        }
    }

    /**
     * 私聊信息
     *
     * @param channel
     * @param content
     * @param message
     * @throws Exception
     */
    @RabbitListener(queues = RabbitConstant.QUEUE_E)
    public void processPrivate(Channel channel, @Payload String content, Message message) throws Exception {
        logger.info(">>>>>>>>>>>>>>>>>message:{}", content);
        IMRecvInfo imRecvInfos =  JsonXMLUtils.json2obj(content, IMRecvInfo.class);
        try {
           // long deliveryTag = message.getMessageProperties().getDeliveryTag();
            //异步处理消息
            processorMessage(imRecvInfos, IMCmdType.PRIVATE_MESSAGE.code());
           //channel.basicAck(deliveryTag,true);
        } catch (Exception e) {
            logger.error(">>>>>>>>>>>>>>>>>error:{}" , e.getMessage());
        }
    }

    /**
     * 处理消息
     * 可采用线程池
     *
     * @param imRecvInfo
     * @param code
     */
    @Async("taskExecutorConfig")
    public void processorMessage(IMRecvInfo imRecvInfo, Integer code) {
        AbstractMessageProcessor processor = ProcessorFactory.createProcessor(IMCmdType.fromCode(code));
        processor.process(imRecvInfo);
    }
}
